package com.mascotikas.server.implementations;

import java.util.Date;
import java.util.List;

import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.mascotikas.client.jdos.ImageInfo;
import com.mascotikas.client.services.IUserImageService;
import com.mascotikas.server.dao.TagDao;
import com.mascotikas.server.dao.UploadedImageDao;
import com.mascotikas.shared.Tag;
import com.mascotikas.shared.UploadedImage;

@SuppressWarnings("serial")
public class UserImageServiceImpl extends RemoteServiceServlet implements
        IUserImageService {

    @Override
    public String getBlobstoreUploadUrl() {
        BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
        return blobstoreService.createUploadUrl("/upload");
    }

    @Override
    public UploadedImage get(String key) {
        UploadedImageDao dao = new UploadedImageDao();
        UploadedImage image = dao.get(key);
        return image;
    }

    @Override
    public List<UploadedImage> getRecentlyUploaded() {
        UploadedImageDao dao = new UploadedImageDao();
        List<UploadedImage> images = dao.getRecent();
        return images;
    }
     @Override
    public List<UploadedImage> getRecentlyUploadedByCategory(String category) {
        UploadedImageDao dao = new UploadedImageDao();
        List<UploadedImage> images = dao.getRecentByCategory(category);
        return images;
    }
    

    @Override
    public void deleteImage(String key) {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        UploadedImageDao dao = new UploadedImageDao();
        UploadedImage image = dao.get(key);
        if (image.getOwnerId().equals(user.getUserId())) {
            dao.delete(key);
        }
    }

    public String tagImage(Tag tag) {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        TagDao dao = new TagDao();

        // TODO: Do validation here of x, y, ImageId

        tag.setTaggerId(user.getUserId());
        tag.setCreatedAt(new Date());

        String key = dao.put(tag);
        return key;
    }

    @Override
    public List<Tag> getTagsForImage(UploadedImage image) {
        TagDao dao = new TagDao();
        List<Tag> tags = dao.getForImage(image);
        return tags;
    }

    public String setDataImage(ImageInfo infoImage) {
        UploadedImageDao dao = new UploadedImageDao();
        return dao.putImageInfo(infoImage);
    }

    public List<UploadedImage> getImagesByOwnerId(String ownerId) {
        UploadedImageDao dao = new UploadedImageDao();
        return dao.getImagesByOwnerId(ownerId);
    }

    public UploadedImage updateUploadImageCategory(String key,String category) {
        UploadedImageDao dao = new UploadedImageDao();
        return dao.updateUploadImageCategory(key,category);

    }
}
